<?php
namespace Addons\Wechat\Controller;
class FeedsController extends \Addon
{
    public function __construct()
    {
        parent::__construct();
        $this->feeds_type = BoxModel('addon_wx_feeds_type');
        $this->feeds = BoxModel('addon_wx_feeds');
        $this->id = I('id', '', 'intval');
    }

    /**
     * [index 推送管理首页]
     *
     */
    public function index()
    {
        $where = "store_id='$this->admin_store_id'";
        $kw = I('get.kw', '', 'trim');
        if ($kw) {
            $where .= ' and `name` like "%' . $kw . '%" or `keyName` like "%' . $kw . '%"';
        }
        /*****数据初始化*******/
        $condition = "keyName='firstConcern' and store_id='$this->admin_store_id'";
        $r = $this->feeds_type->where($condition)->find();
        if (!$r) {
            $row = array(
                'keyName' => 'firstConcern',
                'store_id' => $this->admin_store_id,
                'name' => '首次关注',
                'text' => '欢迎关注我们啊啊！！！！',
            );
            $this->feeds_type->add($row);
        }
        /*********到这里*******/
        $order = 'id desc';
        $this->setPager($this->feeds_type, 12, $where);
        $this->data = $this->feeds_type->where($where)->order($order)->limit($this->limit)->select();
        $this->admin_display();
    }

    public function addType()
    {
        if ($this->id) {
            $this->data = $this->feeds_type->where(array('id' => $this->id))->find();

        }
        $this->admin_display();
    }

    public function checkKey()
    {
        $find = $this->feeds_type->where(array('keyName' => I('keyName')))->find();
        if ($find) {
            echo 'false';
        } else {
            echo 'true';
        }
    }

    public function insertType()
    {
        $keyName = I('keyName');
        S($keyName, NULL);
        if (IS_POST) {
            $data = array(
                'name' => I('name'),
                'keyName' => I('keyName'),
                'store_id' => $this->admin_store_id,
                'text' => $_POST['text'],
            );
            if (!$this->id) {
                $this->feeds_type->add($data);
                $this->jump("操作成功");
            } else {
                $this->feeds_type->where(array('id' => $this->id, 'store_id' => $this->admin_store_id))->save($data);
                $this->jump("操作成功");
            }
        }
    }

    /**
     * 删除分类
     * @return [type]
     */
    public function deleteType()
    {
        $keyName = I('keyName');
        S($keyName, NULL);
        $this->feeds_type->where(array('id' => $this->id, 'store_id' => $this->admin_store_id))->delete();
        $this->jump("操作成功");

    }

    public function feedslist()
    {
        //  $this->memuNav="index";
        $keyName = I('keyName');
        $where = "store_id='$this->admin_store_id'";
        $order = 'id desc';
        $where .= " and keyName='$keyName'";
        $this->feeds_data = $this->feeds_type->where("keyName='$keyName'")->find();
        $this->data = $this->feeds->where($where)->order($order)->select();
        $this->keyName = $keyName;
        $this->admin_display();
    }

    /**
     *  添加推送
     */
    public function addFeeds()
    {
        //  $this->memuNav="index";
        // dump($this->feeds);
        $keyName = I('keyName');
        $this->feeds_data = $this->feeds_type->where("keyName='$keyName'")->find();
        if ($this->id) {
            $this->data = $this->feeds->where(array('id' => $this->id))->find();
        }
        $this->admin_display();
    }

    public function feedsInsert()
    {
        $keyName = I('keyName');
        S($keyName, NULL);
        if (IS_POST) {
            if (empty($this->id)) {
                $info = $this->_upload("/feeds/");
                $data['keyName'] = I('keyName');
                $data['url'] = urlSet(I('url'));
                $data['tip'] = I('tip');
                $data['img'] = $info['img']['savepath'] . $info['img']['savename'];
                $data['store_id'] = $this->admin_store_id;
                $data['title'] = I('title');
                $this->feeds->add($data);
                $this->redirect(addons_url('feedslist', array('keyName' => $data['keyName'], 'memu' => 'System')));
            } else {
                $info = $this->_upload("/feeds/");
                if ($info == false) {
                    $arr['img'] = I('oldimg');
                } else {
                    $arr['img'] = $info['img']['savepath'] . $info['img']['savename'];
                }
                $arr['url'] = (I('url'));
                $arr['tip'] = I('tip');
                $keyName = I('keyName');
                $arr['title'] = I('title');
                $this->feeds->where(array('id' => $this->id, 'store_id' => $this->admin_store_id))->save($arr);
                $this->redirect(addons_url('feedslist', array('keyName' => $keyName, 'memu' => 'System')));
            }
        }
    }

    /**
     * delete 删除
     * @return [type]
     */
    public function deleteFeeds()
    {
        $keyName = I('keyName');
        S($keyName, NULL);
        $this->feeds->where(array('id' => $this->id, 'store_id' => $this->admin_store_id))->delete();
        $this->jump("删除成功");
        //$this->redirect(U('feedslist',array('keyName'=>I('keyName'))));
    }


}

?>